using BitShuva.Models;
using Raven.Client;
using Raven.Client.Linq;
using System;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;

namespace BitShuva.Controllers
{
    public class DurandalController : RavenController
    {
        public DurandalController()
        {
        }

        public async Task<ActionResult> Index()
        {
            var userCookie = HttpContext.Request.Cookies["userId"];
            if (userCookie == null)
            {
                var user = new User() { RegistrationDate = DateTime.UtcNow };
                await this.Session.StoreAsync(user);
                await this.Session.SaveChangesAsync();
                HttpContext.Response.SetCookie(new HttpCookie("userId", user.Id));
            }

            return View();
        }

        [HttpPut]
        public async Task<ActionResult> SampleData()
        {
            var hasExistingData = await this.Session.Query<Song>().AnyAsync();
            if (!hasExistingData)
            {
                var songs = new[]
                {
                    new Song
                    {
                        Album = "Into the Breach",
                        AlbumArtUri = new Uri("http://bitshuvacdn.com/sample/albumart/1.jpg"),
                        Artist = "Zilch",
                        CommunityRank = 7,
                        CommunityRankStanding = CommunityRankStanding.Normal,
                        Id = "songs/sample1",
                        Name = "Chagrin",
                        Number = 5,
                        SongLike = LikeStatus.None,
                        UploadDate = DateTime.Now,
                        Uri = new Uri("http://bitshuvacdn.com/sample/music/1.mp3")
                    },
                    new Song
                    {
                        Album = "RectoVerso",
                        AlbumArtUri = new Uri("http://bitshuvacdn.com/sample/albumart/2.jpg"),
                        Artist = "Electron7",
                        CommunityRank = 12,
                        CommunityRankStanding = CommunityRankStanding.Normal,
                        Id = "songs/sample2",
                        Name = "Digital Zanzibar",
                        Number = 2,
                        SongLike = LikeStatus.None,
                        UploadDate = DateTime.Now,
                        Uri = new Uri("http://bitshuvacdn.com/sample/music/2.mp3")
                    },
                    new Song
                    {
                        Album = "Virtual Trance Drive",
                        AlbumArtUri = new Uri("http://bitshuvacdn.com/sample/albumart/3.jpg"),
                        Artist = "XL Acid 3D",
                        CommunityRank = 42,
                        CommunityRankStanding = CommunityRankStanding.Normal,
                        Id = "songs/sample3",
                        Name = "Emperor's Assault",
                        Number = 2,
                        SongLike = LikeStatus.None,
                        UploadDate = DateTime.Now,
                        Uri = new Uri("http://bitshuvacdn.com/sample/music/3.mp3")
                    }
                };
                var albums = songs.Select((s, index) => new Album
                {
                    AlbumArtUri = s.AlbumArtUri,
                    Artist = s.Artist,
                    Name = s.Album,
                    Id = "albums/sample" + index.ToString()
                }).ToList();

                var storeTasks = songs
                    .Cast<object>()
                    .Concat(albums)
                    .Select(o => this.Session.StoreAsync(o));
                await Task.WhenAll(storeTasks);
            }

            return Json("Success");
        }
    }
}